package www.golf.web.gonwubu;

import java.sql.Date;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.ListitemRenderer;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

import www.golf.ibatis.bean.A_supervision;
import www.golf.ibatis.bean.Projectset;
import www.golf.web.session.WebSession;
import www.golf.web.util.UtilListbox;
import www.golf.web.util.UtilZKObject;

public class A_supervisionControl extends WebSession{
	UtilZKObject ZKObj =new UtilZKObject(this);
	UtilListbox lb= new UtilListbox();
	SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
	public void initialize() {
		List list=null;
		try {
			list = (List)dbSession().queryForList("A_supervision.selectAgreement");
			dbSessionClose();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		lb.setComponentData(this, "grid", list,dataRender());
		set_power();
	}
	public void set_power(){
		boolean group=false;
		for(int i=0;i<getUserGroup().length;i++){
			if(getUserGroup()[i]==2||getUserGroup()[i]<0){
				group=true;
			}
		}
		if(getUserPower()<=13&&group){
			getFellow("add").setVisible(true);
			getFellow("edit").setVisible(true);
		}
	}
	public ListitemRenderer dataRender(){
		 return new ListitemRenderer(){
				public void render(Listitem item, Object data) throws Exception {
					A_supervision form = (A_supervision) data;
					
					Listcell Serial = new Listcell();
					Serial.setLabel(form.getSerial_num());
					Serial.setParent(item);	
					
					Listcell name = new Listcell();
					name.setLabel(form.getName());
					name.setParent(item);		
					
					Listcell subcontractor = new Listcell();
					subcontractor.setLabel(form.getSupervision_name());
					subcontractor.setAttribute("pid", form.getSupervision_id());
					subcontractor.setParent(item);		
					
					Listcell note = new Listcell();
					note.setLabel(form.getNote());
					note.setParent(item);	
					
					Listcell signer = new Listcell();
					signer.setLabel(form.getSigner_name());
					signer.setAttribute("pid", form.getSigner());
					signer.setParent(item);	
					
					Listcell edate = new Listcell();
					edate.setLabel(setDate(form.getS_date()));
					edate.setParent(item);	
					
					Listcell sdate = new Listcell();
					sdate.setLabel(setDate(form.getE_date()));
					sdate.setParent(item);	
					
					Listcell price = new Listcell();
					price.setLabel(String.valueOf(form.getPrice()));
					price.setParent(item);	

					Listcell pid = new Listcell();
					pid.setLabel(String.valueOf(form.getPid()));
					pid.setParent(item);
					pid.setVisible(false);
				}		
			};
	}
	
	public void onEdit(){
		String value=lb.getPKListcellValue(this, "grid");
		if(value!=null){
			try {
				final int key =Integer.parseInt(value);
				final Window win=(Window)Executions.createComponents(getPagefileSession()+"edit.zul", null, null);
				final UtilZKObject zobj= new UtilZKObject(win);
				Events.postEvent("onModal", win, null);
				setListData(win);
				final String pro_list=setEditdata(zobj);
				win.getFellow("ed_submit").addEventListener("onClick", new EventListener(){
					public void onEvent(Event event) throws Exception {
						A_supervision sb=setBean(zobj);
						sb.setSupervision_project((sb.getSupervision_project().equals(pro_list))?
								null:sb.getSupervision_project());
						sb.setPid(key);
						dbSession().update("A_supervision.updateAgreement", sb);
						Messagebox.show("修改完毕");
						win.detach();
						initialize();
					}
				});
				win.getFellow("ed_cancel").addEventListener("onClick",new EventListener(){
					public void onEvent(Event event) throws Exception {
						win.detach();
					}
				});
			} catch (Exception e) {
				e.printStackTrace();
			} 
		}
	}
	public void onDelete(){
		String value=lb.getPKListcellValue(this, "grid");
		if(value!=null){
			int key =Integer.parseInt(value);
			try {
				if(Messagebox.show("删除可能造成系统混乱", "注意", Messagebox.YES | Messagebox.NO, Messagebox.QUESTION )==Messagebox.YES){
					dbSession().delete("A_supervision.deleteAgreement",key);
					initialize();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	public void onAdd() {
		final Window win=(Window)Executions.createComponents(getPagefileSession()+"edit.zul", null, null);
		final UtilZKObject zobj= new UtilZKObject(win);
		win.setTitle("新增資料");
		Events.postEvent("onModal", win, null);
		setListData(win);
		win.getFellow("ed_submit").addEventListener("onClick", new EventListener(){
			public void onEvent(Event event) throws Exception {
				dbSession().update("A_supervision.insertAgreement", setBean(zobj));
				Messagebox.show("新增完畢");
				win.detach();
				initialize();
			}
		});
		win.getFellow("ed_cancel").addEventListener("onClick",new EventListener(){
			public void onEvent(Event event) throws Exception {
				win.detach();
			}
		});
	}
	public String setEditdata(UtilZKObject zobj){
		zobj.setObjectValue("ed_serial_num", lb.getListcellValue(this, "grid", 0));
		zobj.setObjectValue("ed_name", lb.getListcellValue(this, "grid", 1));
		zobj.setObjectValue("ed_supervision", lb.getListcellAttributeValue(this, "grid", 2,"pid"));
		zobj.setObjectValue("ed_note", lb.getListcellValue(this, "grid", 3));
		zobj.setObjectValue("ed_signer", lb.getListcellAttributeValue(this, "grid", 4,"pid"));
		zobj.setObjectValue("ed_sdate", lb.getListcellValue(this, "grid", 5));
		zobj.setObjectValue("ed_edate", lb.getListcellValue(this, "grid", 6));
		zobj.setObjectValue("ed_price", lb.getListcellValue(this, "grid", 7));
		String get_pro_list="";
		try {
			List ps=dbSession().queryForList("A_supervision.select_supervision_project_List",Integer.parseInt(lb.getPKListcellValue(this, "grid")));
			for(Object obj:ps){
				String pro_id=String.valueOf(((Map)obj).get("project_id"));
				get_pro_list=(String)(get_pro_list.equals("")?pro_id:get_pro_list+","+pro_id);
				Listbox lb=(Listbox) zobj.getContrain().getFellow("ed_project");
				for(Object item:lb.getItems()){
					Listitem l_item=(Listitem)item;
					if(pro_id.equals(l_item.getValue())){
						l_item.setSelected(true);
					}
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return get_pro_list;
	}
	public A_supervision setBean(UtilZKObject zobj) throws Exception{
		A_supervision sb= new A_supervision();
		sb.setName(zobj.getValue("ed_name"));
		sb.setSerial_num(zobj.getValue("ed_serial_num"));
		sb.setSupervision_id(Integer.parseInt(zobj.getValue("ed_supervision")));
		sb.setNote(zobj.getValue("ed_note"));
		sb.setSigner(Integer.parseInt(zobj.getValue("ed_signer")));
		sb.setS_date((zobj.getValue("ed_sdate").equals(""))?null:new Date(sdf.parse(zobj.getValue("ed_sdate")).getTime()));
		sb.setE_date((zobj.getValue("ed_edate").equals(""))?null:new Date(sdf.parse(zobj.getValue("ed_edate")).getTime()));
		String project_list="";
		Listbox lb=(Listbox)zobj.getContrain().getFellow("ed_project");
		Iterator it=lb.getSelectedItems().iterator();
		while(it.hasNext()){
			project_list=(String)(project_list.equals("")?((Listitem)it.next()).getValue():project_list+","+((Listitem)it.next()).getValue());
		}
		sb.setSupervision_project(project_list);
		return sb;
	}
	public void setListData(Window win){
		setList(win,"Project.select_List","ed_project");
		setList(win,"Supervision.select_List","ed_supervision");
		setList(win,"User.select_List","ed_signer");
	}
	public String setDate(Date date){
		return date!=null?sdf.format(date):null;
	}
	
}
